package com.zw.admin.server.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zw.admin.server.page.table.PageTableRequest;
import com.zw.admin.server.page.table.PageTableHandler;
import com.zw.admin.server.page.table.PageTableResponse;
import com.zw.admin.server.service.SupplierService;
import com.zw.admin.server.page.table.PageTableHandler.CountHandler;
import com.zw.admin.server.page.table.PageTableHandler.ListHandler;
import com.alibaba.fastjson.JSONObject;
import com.zw.admin.server.component.RegionComponent;
import com.zw.admin.server.dao.PSupplierDao;
import com.zw.admin.server.dto.IntKeyValueDto;
import com.zw.admin.server.dto.ResponseInfo;
import com.zw.admin.server.model.PSupplier;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/pSuppliers")
public class PSupplierController {
	
	private static final Logger log = LoggerFactory.getLogger("adminLogger");
	
    @Autowired
    private PSupplierDao pSupplierDao;

    @Autowired
    private SupplierService supplierService;
    
    @Autowired
    private RegionComponent regionComponent;
    
    @PostMapping
    @ApiOperation(value = "保存供应商")
    @RequiresPermissions("prj:goods")
    public PSupplier save(@RequestBody PSupplier pSupplier) {
        pSupplierDao.insert(pSupplier);

        return pSupplier;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public PSupplier get(@PathVariable Long id) {
    	PSupplier pSupplier =pSupplierDao.getById(id);
    	regionComponent.setRegionTitle(pSupplier);
        return pSupplier;
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public PSupplier update(@RequestBody PSupplier pSupplier) {
    	//自动判断是 新建还是 修改.
    	if(pSupplier.getId()==null) {
    		pSupplierDao.insert(pSupplier);
    	}else {
    		pSupplierDao.update(pSupplier);
    	}
        return pSupplier;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    @RequiresPermissions("prj:goods")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return pSupplierDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<PSupplier> list(PageTableRequest request) {
            	return supplierService.list(request);
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        pSupplierDao.delete(id);
    }
    
    @GetMapping("/getRegion")
    @ApiOperation(value = "取地区")
    public List<IntKeyValueDto> getRegion() {
    	return supplierService.getRegion(null);
    }
    
    @GetMapping("/getRegion/{regId}")
    @ApiOperation(value = "取地区")
    public List<IntKeyValueDto> getRegion(@PathVariable String regId) {
    	return supplierService.getRegion(regId);
    }
    
    
    
    @RequestMapping(value = "/listAll", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "列表")
    public ResponseInfo listAll(HttpServletRequest request) {
    	JSONObject requestJo= new JSONObject();
    	if(request.getParameterMap()!=null) {
    		for(String key :  request.getParameterMap().keySet()) {
    			requestJo.put(key, request.getParameterMap().get(key)[0] );
    		}
    	}
    	
    	List<PSupplier> list = supplierService.listAll(requestJo);
    	JSONObject data= new JSONObject();
		data.put("list", list);
    	return new ResponseInfo("0","",data);
    }
    
}
